package com.hung.threadpool;

import java.util.concurrent.*;

public class ThreadPoolExecutorDemo {

    public static void main(String[] args)
    {
       t5();
    }

    private static void t5() {
        //固定数量线程池-无解队列
        ExecutorService es =Executors.newFixedThreadPool(5);
    }

    private static void t4() {
        int corePoolSize = 5;
        int maxSize = 5;
        ThreadFactory threadFactory=  new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"myThread-%d");

            }
        };
        LinkedBlockingQueue queue = new LinkedBlockingQueue<>(4);
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, maxSize, 60, TimeUnit.SECONDS, queue,threadFactory);
        for (int i = 0; i < 19 ; i++)
        {
            threadPool.execute(
                    threadFactory.newThread(()->{
                        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    }));
            System.out.println("线程池中活跃的线程数： " + threadPool.getPoolSize());
            if (queue.size() > 0)
            {
                System.out.println("----------------队列中阻塞的线程数" + queue.size());
            }
        }

        threadPool.shutdown();
    }

    private static void t3() {
        int corePoolSize = 3;
        int maxSize = 5;
        ThreadFactory threadFactory=  new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"myThread-%d");

            }
        };
        LinkedBlockingQueue queue = new LinkedBlockingQueue<>(4);
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(corePoolSize, maxSize, 60, TimeUnit.SECONDS, queue,threadFactory);

        for (int i = 0; i < 19 ; i++)
        {
            threadPool.execute(
                    threadFactory.newThread(()->{
                        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    }));
            System.out.println("线程池中活跃的线程数： " + threadPool.getPoolSize());
            if (queue.size() > 0)
            {
                System.out.println("----------------队列中阻塞的线程数" + queue.size());
            }
        }

        threadPool.shutdown();
    }

    private static void t2() {
        LinkedBlockingQueue<Runnable> queue =
                new LinkedBlockingQueue<Runnable>(3);
        RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 5, 60, TimeUnit.SECONDS, queue,handler);
        for (int i = 0; i < 9 ; i++)
        {
            threadPool.execute(
                    new Thread(new ThreadPoolTest(), "Thread".concat(i + "")));
            System.out.println("线程池中活跃的线程数： " + threadPool.getPoolSize());
            if (queue.size() > 0)
            {
                System.out.println("----------------队列中阻塞的线程数" + queue.size());
            }
        }
        threadPool.shutdown();
    }

    private static void t1() {
        LinkedBlockingQueue<Runnable> queue =
                new LinkedBlockingQueue<Runnable>(7);
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 13, 60, TimeUnit.SECONDS, queue);
        for (int i = 0; i < 31 ; i++)
        {
            threadPool.execute(new Thread(new ThreadPoolTest(), "Thread".concat(i + "")));
            System.out.println("线程池中活跃的线程数： " + threadPool.getPoolSize()+"，队列中阻塞的线程数："+queue.size()+"，总任务数："+threadPool.getTaskCount());

        }
        threadPool.shutdown();
    }

    static class ThreadPoolTest implements Runnable {
        @Override
        public void run() {
            try {
                System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}